#######		REPLICATION FILES
#######		Climate Variability and Irregular Migration to the European Union
#######		Global Environmental Change
#######		Fabien Cottier and Idean Salehyan
#######		Replication: Figure 2: plot displaying trends in irregular migration 2010-2015
#######		This version: April 2021




# Set working directory
setwd("path/to/replication/directory")


# Load packages
library(dplyr)
library(tidyverse)


# load data
frontex <- readr::read_csv("FrontexIBCP_2009_2017m_cntr_df.csv")


# aggre data to monthly format
mtotal_df <- frontex %>%
  dplyr::filter(year %in% 2010:2015) %>%
  dplyr::group_by(year, monthN) %>%
  dplyr::summarise(
    monthA = first(monthA),
    quarter = first(quarter), 
    nmigr = sum(nmigr, na.rm = T),
    nmigr_exbalk = sum(nmigr_exbalk ,na.rm = T) 
  ) %>%
  dplyr::arrange(year, monthN)

munk_df <- frontex %>%
  dplyr::filter (cowc<0, year %in% 2010:2015) %>%
  dplyr::group_by(year, monthN) %>%
  dplyr::summarise(
    monthA = first(monthA),
    quarter = first(quarter),
    nmigr = sum(nmigr, na.rm = T),
    nmigr_exbalk = sum(nmigr_exbalk, na.rm = T)
  ) %>%
  dplyr::arrange(year, monthN)

# add date field
mtotal_df$date <- as.Date(paste(mtotal_df$year,mtotal_df$monthA,1),format = "%Y %b %d")
munk_df$date <- as.Date(paste(munk_df$year,munk_df$monthA,1),format = "%Y %b %d")

# create plot
plot.new()
plot.window(
  xlim = c(as.Date(c("2010-01-01","2015-12-01"))),
  ylim = c(1E2,1E6),
  log = "y"
)
axis.Date(
  1,
  at = seq(as.Date("2010-01-01"), as.Date("2016-01-01"), "year")
)
at.y <- outer(1:9, 10^(2:6))[1:37]
lab.y <- ifelse(log10(at.y) %% 1 == 0, at.y, NA)
axis(
  2,
  at = at.y,
  labels = F
)
axis(
  2,
  at = c(1E2,1E3,1E4,1E5,1E6),
  labels = c("100","1,000","10,000","100,000","1,000,000"),
  lwd.ticks = 2
)
lines(mtotal_df$date, mtotal_df$nmigr_exbalk, lwd = 2)
lines(munk_df$date, munk_df$nmigr_exbalk, lwd = 2, lty = 2)
